<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php



class MPartDownloadController
    extends MApplicationComponent 
    implements MIController
{
    
    public function invoke()
    {
        FXP::trace(FXP::t('Begin to process {class}::{function}',
            array('{class}'=>get_class($this), '{function}'=>__FUNCTION__)));
                                parent::init();
                                $keys = array ('Filename','key');
        if (MSecurity :: verification($keys, $_POST) == false) {
            FXP::log(FXP::t("Request is Error, keys:'{$keys}'"), MLogger::LEVEL_ERROR);
            throw new MException(FXP::t(MConst::FILE_NOT_EXIST), MConst::DOWNLOAD_FILE_FAILS);
        }
        $this->downloadFile();
    }
    
    
    private function downloadFile() {
        $file_name  = $_POST["Filename"];
        $key        = $_POST["key"];
        $path       = '';
                                if (strlen(trim($file_name)) <= 0 || strlen(trim($key)) <= 0) {
            FXP::log(FXP::t("Request is Error, file_name:'{$file_name}'"), MLogger::LEVEL_ERROR);
            throw new MException(FXP::t(MConst::FILE_NOT_EXIST), MConst::DOWNLOAD_FILE_FAILS);
        }
                                $path = str_replace("\${filename}", $file_name, $key);
        if (is_null($path) || strlen(trim($path)) <= 0) {
            FXP::log(FXP::t("Request is Error, file_name:'{$file_name}'"), MLogger::LEVEL_ERROR);
            throw new MException(FXP::t(MConst::FILE_NOT_EXIST), MConst::DOWNLOAD_FILE_FAILS);
        }
    
        $file_path = DOCUMENT_ROOT_BLOCK . $path;
    
                if (file_exists($file_path) == false) {
            FXP::log(FXP::t("File do not exist, path:'{$file_path}'"), MLogger::LEVEL_ERROR);
            throw new MException(FXP::t(MConst::FILE_NOT_EXIST), MConst::DOWNLOAD_FILE_FAILS);
        }
    
        $content_type = 'application/force-download';
        MUtils::download($file_path, $content_type, $file_name);
    }
    
    
    public function handleException($exception)
    {
        return header("HTTP/1.1 ". $exception->getCode());
    }

}

?>